﻿using System;
using System.Data;
using DataAccessObject;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.FUNC;

namespace ThangNM
{
    public partial class Topic : System.Web.UI.Page
    {
        public ThangNM.PublicInterface.Home_New mtpHome;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                mtpHome = Master as ThangNM.PublicInterface.Home_New;
                mtpHome.setClearMsg();
                if (!IsPostBack)
                    BindData();
            }
            catch (Exception ex)
            {
                if (mtpHome != null)
                    mtpHome.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            if (Request.QueryString[QueryString.topic] != null)
            {
                TopicEO _TopicEO = new TopicEO();
                DataSet dsAll = null;
                Int32 iUID = 0;
                if (Int32.TryParse(StringEx.Trim(StringEx.Trim(Request.QueryString[QueryString.topic])), out iUID))
                {
                    _TopicEO.PK_iTopicID = iUID;
                    dsAll = (new TopicDAO()).SelectBy_PK_iTopicID(_TopicEO.PK_iTopicID);
                }
                if (DataTableEx.DataTable_OK(null, dsAll, 0) == false)
                {
                    Commons.setRedirect(Page, null);
                    return;
                }
                _TopicEO = (new TopicDAO()).TopicDO(dsAll.Tables[0]);
                if (_TopicEO == null)
                {
                    Commons.setRedirect(Page, null);
                    return;
                }
                if (!StringEx.Compare(Request.RawUrl.Substring(1), _TopicEO.sUrlRewrite))
                {
                    Commons.setRedirect(Page, _TopicEO.sUrlRewrite);
                    return;
                }
                DataTable dtAtt = null, dtOther = null;
                if (DataTableEx.DataTable_OK(null, dsAll, 1))
                    dtAtt = dsAll.Tables[1];
                if (DataTableEx.DataTable_OK(null, dsAll, 2))
                    dtOther = dsAll.Tables[2];
                Commons.setSEO_ON_PAGE(Page, _TopicEO, this.GetType());
                ArticleUC1.BindData_Topic(_TopicEO);
                ArticleUC1.BindData_Common(dtAtt, dtOther, _TopicEO, true);
            }
            else
            {
                Commons.setRedirect(Page, null);
                return;
            }
        }
    }
}